package com.stark.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.stark.core.domain.User;
import com.stark.core.query.UserCondition;
import com.stark.core.validator.InsertGroup;
import com.stark.core.validator.UpdateGroup;
import com.stark.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@PostMapping
	@ApiOperation("新增用户")
	public User add(@RequestBody @Validated(InsertGroup.class) User user) {
		user.setId(null);
		return userService.save(user);
	}
	
	@DeleteMapping("/{id}")
	@ApiOperation("删除用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataTypeClass = Long.class, required = true)
	})
	public void delete(@PathVariable Long id) {
		userService.delete(id);
	}
	
	@PutMapping("/{id}")
	@ApiOperation("更新用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataTypeClass = Long.class, required = true)
	})
	public User update(@PathVariable Long id, @RequestBody @Validated(UpdateGroup.class) User user) {
		return userService.save(user);
	}
	
	@GetMapping("/{id}")
	@ApiOperation("获取用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataTypeClass = Long.class, required = true)
	})
	public User get(@PathVariable Long id) {
		return userService.get(id);
	}
	
	@GetMapping("/count")
	@ApiOperation("统计用户数量")
	public long count(UserCondition condition) {
		return userService.count(condition);
	}
	
	@GetMapping("/list")
	@ApiOperation("查询用户列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sort", value = "排序规则，以 property,order 的方式组织，可以传多组", paramType = "query", dataTypeClass = String.class, required = false, defaultValue = "id,asc")
	})
	public List<User> getList(UserCondition condition, Sort sort) {
		return userService.getList(condition, sort);
	}
	
	@GetMapping("/page")
	@ApiOperation("查询用户分页数据")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "分页页码", paramType = "query", dataTypeClass = Integer.class, required = false, defaultValue = "0"),
		@ApiImplicitParam(name = "size", value = "单页记录数", paramType = "query", dataTypeClass = Integer.class, required = false, defaultValue = "10"),
		@ApiImplicitParam(name = "sort", value = "排序规则，以 property,order 的方式组织，可以传多组", paramType = "query", dataTypeClass = String.class, required = false, defaultValue = "id,asc")
	})
	public Page<User> getPage(UserCondition condition, Pageable pageable) {
		return userService.getPage(condition, pageable);
	}

}
